package com.tongji.admin.view
{
	import com.tongji.admin.event.TypeAdminEvent;
	import com.tongji.admin.model.DictModel;
	import com.tongji.admin.vo.Type;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.events.IndexChangedEvent;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/7/
	 */ 
	public class DictAdminMediator extends Mediator
	{
		[Inject]
		public var view:DictAdminView;
		
		[Inject]
		public var model:DictModel;
		
		override public function onRegister():void
		{
			view.saveBtn.addEventListener(MouseEvent.CLICK, addTypeHandler);			
			BindingUtils.bindProperty(view.goodsType, "dataProvider", model, "goodsTypes");
			view.typeNav.addEventListener(IndexChangedEvent.CHANGE, test);
		}
		
		private function test(event:IndexChangedEvent):void
		{
			switch(view.typeNav.selectedIndex)
			{
				case 0:
					
					break;
				case 1:
					view.cityType.dataProvider = model.cityTypes;
					break;
				case 2:
				    view.picType.dataProvider = model.picTypes;
					break;
			}
		}
		
		private function addTypeHandler(event:MouseEvent):void
		{
			var newType:Type = new Type();
			newType.dictID = 0;
			newType.parentID = 0;
			newType.typeCreateDate = new Date();
			newType.typeCreator = "caddy";
			newType.typeName = view.typeName.text;
			switch(view.typeNav.selectedIndex)
			{
				case 0:
					newType.typeFlag = 0;
					break;
				case 1:
					newType.typeFlag = 1;
					break;
				case 2:
					newType.typeFlag = 2;
					break;
			}
			
			dispatch(new TypeAdminEvent(TypeAdminEvent.ADD_TYPE, newType));
		}
	}
}